package com.servlet;

import com.bean.Admin;
import com.bean.Student;
import com.bean.Teacher;
import com.service.AdminService;
import com.service.StudentService;
import com.service.TeacherService;
import com.service.impl.AdminServiceImpl;
import com.service.impl.StudentServiceImpl;
import com.service.impl.TeacherServiceImpl;
import com.utils.WebUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet(name = "UserServlet", urlPatterns = "/UserServlet.do")
public class UserServlet extends BaseServlet {
    AdminService adminService = new AdminServiceImpl();
    StudentService studentService = new StudentServiceImpl();
    TeacherService teacherService = new TeacherServiceImpl();

    protected void getUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Object user = request.getSession().getAttribute("user");
        Map map = new LinkedHashMap();
        String url = "/login.html";
        if (user != null) {
            try {
                user = (Student) user;
                url = "/stuframe.html";
            } catch (Exception e1) {
                try {
                    user = (Teacher) user;
                    url = "/tframe.html";
                } catch (Exception e2) {
                    user = (Admin) user;
                    url = "/frame.html";
                }
            }
            map.put("user", user);
        }
        map.put("url", url);
        response.getWriter().write(WebUtils.parseJson(map));
    }

    protected void loginOut(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().removeAttribute("user");
        response.getWriter().write("");
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String url = "";
        Object user = null;
        Integer tip = -1;
        switch (role) {
            case "student":
                user = new Student(username, password);
                url = "/stuframe.html";
                tip = studentService.login((Student) user);
                break;
            case "teacher":
                user = new Teacher(username, password);
                url = "/tframe.html";
                tip = teacherService.login((Teacher) user);
                break;
            case "admin":
                user = new Admin(username, password);
                url = "/frame.html";
                tip = adminService.login((Admin) user);
                break;
            default:
        }
        if (tip == 0) {
            request.getSession().setAttribute("user", user);
            Cookie cookie = new Cookie("username", username);
            cookie.setMaxAge(7 * 24 * 60 * 60);
            response.addCookie(cookie);
            cookie = new Cookie("role", role);
            cookie.setMaxAge(7 * 24 * 60 * 60);
            response.addCookie(cookie);
        }
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("tip", tip);
        map.put("url", url);
        response.getWriter().write(WebUtils.parseJson(map));
    }

    protected void updatePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            return;
        }
        Integer id;
        int tip;
        String password = request.getParameter("password");
        String newPassword = request.getParameter("newPassword");
        try {
            Student student = (Student) user;
            id = student.getId();
            tip = studentService.updatePassword(id, password, newPassword);
        } catch (Exception e1) {
            try {
                Teacher teacher = (Teacher) user;
                id = teacher.getId();
                tip = teacherService.updatePassword(id, password, newPassword);
            } catch (Exception e2) {
                Admin admin = (Admin) user;
                id = admin.getId();
                tip = adminService.updatePassword(id, password, newPassword);
            }
        }
        if (tip == 0) {
           request.getSession().removeAttribute("user");
        }
        response.getWriter().write(String.valueOf(tip));
    }

    protected void updeteInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            return;
        }
        int tip;
        try {
            Student student = (Student) user;
            WebUtils.copyParamToBean(request.getParameterMap(), student);
            tip = studentService.updeteInfo(student);
            student = new Student();
            student.setId(((Student) user).getId());
            student = studentService.selectBy(student);
            request.getSession().setAttribute("user", student);
        } catch (Exception e1) {
            try {
                Teacher teacher = (Teacher) user;
                WebUtils.copyParamToBean(request.getParameterMap(), teacher);
                tip = teacherService.updeteInfo(teacher);
                if (tip == 0) {
                    teacher = new Teacher();
                    teacher.setId(((Teacher) user).getId());
                    teacher = teacherService.selectBy(teacher);
                    request.getSession().setAttribute("user", teacher);
                }
            } catch (Exception e2) {
                Admin admin = (Admin) user;
                WebUtils.copyParamToBean(request.getParameterMap(), admin);
                tip = adminService.updeteInfo(admin);
                if (tip == 0) {
                    admin = new Admin();
                    admin.setId(((Admin) user).getId());
                    admin = adminService.selectBy(admin);
                    request.getSession().setAttribute("user", admin);
                }
            }
        }
        response.getWriter().write(String.valueOf(tip));
    }

}
